export function pwStrength(pwd) {//显示颜色
  let O_color = "#e5e5e5"
  let L_color = "#FF6252"
  let M_color = "#FFD513"
  let H_color = "#30C060"
  let Lcolor
  let Mcolor
  let Hcolor
  let msg
  if (!pwd.trim()) {
    Lcolor = Mcolor = Hcolor = O_color;
  } else {
    let S_level = checkStrong(pwd);
    switch (S_level) {
      case 0:
        Lcolor = Mcolor = Hcolor = O_color;
        break
      case 1:
        Lcolor = L_color;
        Mcolor = Hcolor = O_color;
        msg = '密码不能使用纯数字,纯字母或纯符号'
        break
      case 2:
        Lcolor = M_color;
        Mcolor = M_color;
        Hcolor = O_color;
        break
      default:
        Lcolor = H_color;
        Mcolor = H_color;
        Hcolor = H_color;
    }
  }
  if (document.querySelector(".strengthA")){
    document.querySelector(".strengthA").style.background = Lcolor;
    document.querySelector(".strengthB").style.background = Mcolor;
    document.querySelector(".strengthC").style.background = Hcolor;
    return msg
  }

}

//判断输入密码的类型
export function CharMode(iN) {
  if (iN >= 48 && iN <= 57){//数字
    return 1
  }else if(iN >= 65 && iN <= 90){//大写
    return 2
  }else if(iN >= 97 && iN <= 122){//小写
    return 4
  }else{
    return 8
  }
}

//bitTotal函数
//计算密码模式
export function bitTotal(num) {
  switch (num){
    case 6:
      num = 1;
      break;
    case 14 || 7:
      num = 5;
      break;
    default:
      break
  }
  let modes = 0;
  for (let i = 0; i < 4; i++) {
    if (num & 1){
      modes++
    }
    num >>>= 1;
  }
  return modes;
}

//返回强度级别
export function checkStrong(sPW) {
  if (sPW.length < 8){//密码太短
    return 0
  }else{
    var Modes = 0;
    for (let i = 0; i < sPW.length; i++) {
      Modes =  Modes | CharMode(sPW.charCodeAt(i));//密码模式
    }
    return bitTotal(Modes);
  }
}
